package com.sihai.ehr.service.impl;

import com.sihai.core.*;
import com.sihai.ehr.model.biz.LeaveOfficeProject;
import com.sihai.ehr.repository.AuditingRepository;
import com.sihai.ehr.service.LeaveOfficeProjectService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @author wxn
 * @since 13-4-26 23:24
 *
 * 离职项目Service
 */
@Service("leaveOfficeProjectService")
public class LeaveOfficeProjectServiceImpl implements LeaveOfficeProjectService {
    @Resource
    private DataAccessor dataAccessor;
    @Resource
    private AuditingRepository auditingRepository;

    @Override
    public void save(LeaveOfficeProject project) throws Exception {
        Assert.notNull(project, "LeaveOfficeProject is required");
        dataAccessor.save(project);
    }

    @Override
    public LeaveOfficeProject get(Long id) throws Exception {
        Assert.notNull(id, "LeaveOfficeProject id is required");
        return dataAccessor.get(LeaveOfficeProject.class, id);
    }

    @Override
    public boolean delete(String ids) throws Exception {
        final String deleteSql = String.format("delete from LeaveOfficeProject lop where lop.id in (%s)", ids);
        this.dataAccessor.batchUpdate(new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                return session.createQuery(deleteSql);
            }
        });
        return true;
    }

    @Override
    public boolean delete(Long id)throws Exception{
        dataAccessor.delete(get(id));
        return true;
    }

    @Override
    public LimitedList<LeaveOfficeProject> findByTopOrganization(Long organizationId, Limit limit, Sorter sorter) throws Exception {
        return dataAccessor.findByHql(LeaveOfficeProject.class, "from LeaveOfficeProject lop where lop.organization.id = ?", new Object[]{organizationId}, limit, sorter);
    }

}
